#include "StdAfx.h"
#include "QvSignalObject.h"

QvSignalObject::QvSignalObject(QvBoolean bManualReset, const QvChar* strName) : m_hEvent(NULL)
{
    if(strName)
    {
        m_hEvent = ::OpenEvent(EVENT_ALL_ACCESS, FALSE, strName);
    }
    if(m_hEvent == NULL)
    {
        m_hEvent = ::CreateEvent(0, bManualReset, FALSE, strName);
    }
}

QvSignalObject::~QvSignalObject(void)
{
    SAFE_CLOSE_HANDLE(m_hEvent);
}

void QvSignalObject::Set(QvBoolean bSignaled)
{
    if(bSignaled)
    {
        ::SetEvent(m_hEvent);
    }
    else
    {
        ::ResetEvent(m_hEvent);
    }
}

QvByte QvSignalObject::WaitSignal(QvULong uMilliseconds)
{
    DWORD dwCode = WaitForSingleObject(m_hEvent, uMilliseconds);
    switch(dwCode)
    {
    case WAIT_OBJECT_0:
        return 1;
    case WAIT_TIMEOUT:
        return -1;
    }
    return 0;
}

